<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: Enumerable</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">Enumerable</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/vendor/rails/activesupport/lib/active_support/core_ext/enumerable_rb.html">
                vendor/rails/activesupport/lib/active_support/core_ext/enumerable.rb
                </a>
        <br />
                <a href="../files/vendor/rails/activesupport/lib/active_support/json/encoders/enumerable_rb.html">
                vendor/rails/activesupport/lib/active_support/json/encoders/enumerable.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M002205">each_with_object</a>&nbsp;&nbsp;
      <a href="#M002203">group_by</a>&nbsp;&nbsp;
      <a href="#M002206">index_by</a>&nbsp;&nbsp;
      <a href="#M002207">many?</a>&nbsp;&nbsp;
      <a href="#M002204">sum</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M002205" class="method-detail">
        <a name="M002205"></a>

        <div class="method-heading">
          <a href="#M002205" class="method-signature">
          <span class="method-name">each_with_object</span><span class="method-args">(memo, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Iterates over a collection, passing the current element <b>and</b> the
<tt>memo</tt> to the block. Handy for building up hashes or reducing
collections down to one object. Examples:
</p>
<pre>
  %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } #=&gt; {'foo' =&gt; 'FOO', 'bar' =&gt; 'BAR'}
</pre>
<p>
<b>Note</b> that you can&#8216;t use immutable objects like numbers, true
or false as the memo. You would think the following returns 120, but since
the memo is never changed, it does not.
</p>
<pre>
  (1..5).each_with_object(1) { |value, memo| memo *= value } # =&gt; 1
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002205-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002205-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/enumerable.rb, line 79</span>
79:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each_with_object</span>(<span class="ruby-identifier">memo</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
80:     <span class="ruby-identifier">returning</span> <span class="ruby-identifier">memo</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">m</span><span class="ruby-operator">|</span>
81:       <span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
82:         <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">element</span>, <span class="ruby-identifier">m</span>)
83:       <span class="ruby-keyword kw">end</span>
84:     <span class="ruby-keyword kw">end</span>
85:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002203" class="method-detail">
        <a name="M002203"></a>

        <div class="method-heading">
          <a href="#M002203" class="method-signature">
          <span class="method-name">group_by</span><span class="method-args">() {|element| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Collect an enumerable into sets, grouped by the result of a block. Useful,
for example, for grouping records by date.
</p>
<p>
Example:
</p>
<pre>
  latest_transcripts.group_by(&amp;:day).each do |day, transcripts|
    p &quot;#{day} -&gt; #{transcripts.map(&amp;:class).join(', ')}&quot;
  end
  &quot;2006-03-01 -&gt; Transcript&quot;
  &quot;2006-02-28 -&gt; Transcript&quot;
  &quot;2006-02-27 -&gt; Transcript, Transcript&quot;
  &quot;2006-02-26 -&gt; Transcript, Transcript&quot;
  &quot;2006-02-25 -&gt; Transcript&quot;
  &quot;2006-02-24 -&gt; Transcript, Transcript&quot;
  &quot;2006-02-23 -&gt; Transcript&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002203-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002203-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/enumerable.rb, line 22</span>
22:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">group_by</span>
23:     <span class="ruby-identifier">assoc</span> = <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">OrderedHash</span>.<span class="ruby-identifier">new</span>
24: 
25:     <span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
26:       <span class="ruby-identifier">key</span> = <span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">element</span>)
27: 
28:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">assoc</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">key</span>)
29:         <span class="ruby-identifier">assoc</span>[<span class="ruby-identifier">key</span>] <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">element</span>
30:       <span class="ruby-keyword kw">else</span>
31:         <span class="ruby-identifier">assoc</span>[<span class="ruby-identifier">key</span>] = [<span class="ruby-identifier">element</span>]
32:       <span class="ruby-keyword kw">end</span>
33:     <span class="ruby-keyword kw">end</span>
34: 
35:     <span class="ruby-identifier">assoc</span>
36:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002206" class="method-detail">
        <a name="M002206"></a>

        <div class="method-heading">
          <a href="#M002206" class="method-signature">
          <span class="method-name">index_by</span><span class="method-args">() {|elem| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Convert an enumerable to a hash. Examples:
</p>
<pre>
  people.index_by(&amp;:login)
    =&gt; { &quot;nextangle&quot; =&gt; &lt;Person ...&gt;, &quot;chade-&quot; =&gt; &lt;Person ...&gt;, ...}
  people.index_by { |person| &quot;#{person.first_name} #{person.last_name}&quot; }
    =&gt; { &quot;Chade- Fowlersburg-e&quot; =&gt; &lt;Person ...&gt;, &quot;David Heinemeier Hansson&quot; =&gt; &lt;Person ...&gt;, ...}
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002206-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002206-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/enumerable.rb, line 94</span>
94:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">index_by</span>
95:     <span class="ruby-identifier">inject</span>({}) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">accum</span>, <span class="ruby-identifier">elem</span><span class="ruby-operator">|</span>
96:       <span class="ruby-identifier">accum</span>[<span class="ruby-keyword kw">yield</span>(<span class="ruby-identifier">elem</span>)] = <span class="ruby-identifier">elem</span>
97:       <span class="ruby-identifier">accum</span>
98:     <span class="ruby-keyword kw">end</span>
99:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002207" class="method-detail">
        <a name="M002207"></a>

        <div class="method-heading">
          <a href="#M002207" class="method-signature">
          <span class="method-name">many?</span><span class="method-args">(&amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns true if the collection has more than 1 element. Functionally
equivalent to collection.size &gt; 1. Works with a block too ala any?, so
people.many? { |p| p.age &gt; 26 } # =&gt; returns true if more than 1
person is over 26.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002207-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002207-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/enumerable.rb, line 103</span>
103:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">many?</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
104:     <span class="ruby-identifier">size</span> = <span class="ruby-identifier">block_given?</span> <span class="ruby-value">? </span><span class="ruby-identifier">select</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>).<span class="ruby-identifier">size</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">size</span>
105:     <span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span>
106:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M002204" class="method-detail">
        <a name="M002204"></a>

        <div class="method-heading">
          <a href="#M002204" class="method-signature">
          <span class="method-name">sum</span><span class="method-args">(identity = 0, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Calculates a <a href="Enumerable.html#M002204">sum</a> from the elements.
Examples:
</p>
<pre>
 payments.sum { |p| p.price * p.tax_rate }
 payments.sum(&amp;:price)
</pre>
<p>
The latter is a shortcut for:
</p>
<pre>
 payments.inject { |sum, p| sum + p.price }
</pre>
<p>
It can also calculate the <a href="Enumerable.html#M002204">sum</a> without
the use of a block.
</p>
<pre>
 [5, 15, 10].sum # =&gt; 30
 [&quot;foo&quot;, &quot;bar&quot;].sum # =&gt; &quot;foobar&quot;
 [[1, 2], [3, 1, 5]].sum =&gt; [1, 2, 3, 1, 5]
</pre>
<p>
The default <a href="Enumerable.html#M002204">sum</a> of an empty list is
zero. You can override this default:
</p>
<pre>
 [].sum(Payment.new(0)) { |i| i.amount } # =&gt; Payment.new(0)
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M002204-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M002204-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/enumerable.rb, line 57</span>
57:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">sum</span>(<span class="ruby-identifier">identity</span> = <span class="ruby-value">0</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
58:     <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">identity</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
59: 
60:     <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
61:       <span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>).<span class="ruby-identifier">sum</span>
62:     <span class="ruby-keyword kw">else</span>
63:       <span class="ruby-identifier">inject</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">sum</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">sum</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">element</span> }
64:     <span class="ruby-keyword kw">end</span>
65:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>